package com.service.impl;

import com.mapper.UserMapper;
import com.pojo.User;
import com.pojo.UserExample;
import com.service.UserService;
import com.util.MD5Util;
import com.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: HL
 * Date: 2020/3/3
 * Time: 14:34
 * Description: No Description
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse save(User user) {
        Byte status =1;
        user.setStatus(status);
     /*   user.setType(0);*/
        //执行mybatis添加方法
        int rows = userMapper.insert(user);
        //判断添加成功
        if (rows > 0)
            return ServerResponse.addSuccess();
        else
            return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse modifyStatus(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        Byte status = 0;
        user.setStatus(status);
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i>0)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }

    @Override
    public ServerResponse modify(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i>0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse getById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user!=null)
            return ServerResponse.getDataSuccess(user);
        return ServerResponse.getDataFailed("该用户信息不存在");
    }

    @Override
    public ServerResponse getAll() {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        Byte status = 1;
        criteria.andStatusEqualTo(status);
        List<User> users = userMapper.selectByExample(example);
        if (users!=null&&users.size()>0)
            return ServerResponse.getDataSuccess(users);
        return ServerResponse.getDataFailed("没有数据");
    }

    // 登录
    @Override
    public ServerResponse getTelnoAndPassword(String telno, String password) {
        System.out.println("telno:"+telno);
        System.out.println("password:"+password);
        String mdPassword = MD5Util.md5(password);
        System.out.println("mdPassword:"+mdPassword);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria= userExample.createCriteria();
        criteria.andUserPsdEqualTo(mdPassword).andUserTelnoEqualTo(telno);
        List<User> userList = userMapper.selectByExample(userExample);
        if (userList != null && userList.size()>0){
            for (User user:userList){
              /* if ( user.getType()==0){
                   //用户未登录
                   user.setType(1);*/
                //返回用户id
                int userId = user.getUserId();
                return ServerResponse.hasLogin(userId);
            }
            return ServerResponse.noLogin();
        }

        return ServerResponse.getDataFailed("用户不存在或用户名密码错误");
    }


    @Override
    public ServerResponse getByTelno(String telno){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria().andUserTelnoEqualTo(telno);
        List<User> userList = userMapper.selectByExample(userExample);
        if (userList != null && userList.size()>0){
            return ServerResponse.hasRegist();
        }else {
            return ServerResponse.noRegist();
        }

    }
}
